package org.lx.nursing.controller.device;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.lx.common.core.controller.BaseController;
import org.lx.common.core.domain.AjaxResult;
import org.lx.common.core.page.TableDataInfo;
import org.lx.nursing.domain.Device;
import org.lx.nursing.dto.DeviceDto;
import org.lx.nursing.service.IDeviceDataService;
import org.lx.nursing.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 华为设备Controller
 *
 * @author sewen
 * @date 2025-03-11
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "华为设备相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;
    @Resource
    private IDeviceDataService deviceDataService;

    /**
     * 查询华为设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation("查询华为设备列表")
    public TableDataInfo<List<Device>> list(Device device) {
        return getDataTable(deviceService.selectDeviceList(device));
    }

    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }


    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public AjaxResult allProduct() {
        return success(deviceService.allProduct());
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult registerDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.registerDevice(deviceDto);
        return success();
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping("/{iotId}")
    @ApiOperation("获取设备详细信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)})
    public AjaxResult getInfo(@PathVariable("iotId") String iotId) {
        return success(deviceService.queryDeviceDetail(iotId));
    }


    /**
     * 查询设备上报数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)
    })
    public AjaxResult queryServiceProperties(@PathVariable("iotId") String iotId) {
        AjaxResult ajaxResult = deviceService.queryServiceProperties(iotId);
        return ajaxResult;
    }


    /**
     * 1.7 修改设备
     * 接口地址:/nursing/device
     * 请求方式:PUT
     */
    @PutMapping
    @ApiOperation("修改华为设备")
    public AjaxResult edit(@RequestBody DeviceDto deviceDto) {
        boolean result = deviceService.updateIDevice(deviceDto);
        if (result) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 1.8 删除设备
     * 接口地址:/nursing/device/{iotId}
     * 请求方式:DELETE
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation("删除华为设备")
    public AjaxResult remove(@PathVariable String iotId) {
        boolean result = deviceService.deleteIDeviceById(iotId);
        if (result) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    @GetMapping("/queryProduct/{productKey}")
    @ApiOperation(value = "查询产品详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "productKey", value = "产品id", required = true, dataTypeClass = String.class)})
    public AjaxResult queryProduct(@PathVariable String productKey) {
        return deviceService.queryProduct(productKey);
    }


}
